﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;

namespace Web
{
    public partial class dk : System.Web.UI.Page
    {
        private string codeMemberParent = string.Empty;
        private int idMember = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GetCodeMember(); // Lấy mã giới thiệu
            MemberInfo info = new MemberInfo();
            FillData(info);
            idMember = MemberBL.Insert(info);
            if (idMember > 0)
            {
                try
                {
                    GetIdMemberTree();
                }
                catch (Exception)
                {
                }
            }
        }

        #region FillData
        void FillData(MemberInfo info)
        {
            info.FullName = txtname.Text;
            info.Address = "";
            info.Email = "";
            info.Status = Const.show;
            info.DateBirth = DateTime.Now;
            info.Pass ="";
            info.PhoneNumber ="";
            info.CodeMemberParent = codeMemberParent; 
            info.CreatedOn = DateTime.Now;
            info.LeftRight = "L";
            info.PriceMount = 0;
            info.Checktype = 0;
            info.CodePosition = "0";
            info.CodeMember = "";
            info.UserName = "";
            info.IsActive = false;
            info.NumberAcount = "";
            info.NameBank = "";
        }
        #endregion

        #region lấy mã giới thiệu Level
        string GetCodeMember()
        {
            try
            {
                MemberInfo info = MemberBL.Select(txtCodeMemberParent.Text, 4);// lấy thông tin thành viên có mã gt
                #region kiểm tra mã giới thiệu
                if (info != null)// Trường hợp có mã gt,
                {
                    codeMemberParent = txtCodeMemberParent.Text.Trim();
                }
                else
                {
                    codeMemberParent = "0";
                }
                return codeMemberParent;
            }
            catch (Exception)
            {
            }
            return codeMemberParent;
                #endregion
        }
        #endregion

        void GetIdMemberTree()
        {
            string Note_RootL = "0";
            string Note_RootR = "0";
            int sumLeft = 0, sumRight = 0;
            string memberId = "0";// mã GT matrix
            MemberNoteTreeInfo info = new MemberNoteTreeInfo();
            info = MemberNoteTreeBL.Select(2, txtCodeMemberParent.Text.Trim(),"");//Kiểm tra mã giới thiệu nhập vào ->Nếu tồn tại lấy IdMember
            if (info != null)
            {               
                memberId = info.IdMember.ToString();
            }
            else // trường hợp không có mã giới thiệu, lấy 1 mã duy nhất
            {
                info = MemberNoteTreeBL.Select(1, "","");
                if (info != null)
                {                  
                    memberId = info.IdMember.ToString();
                }
            }

            CHRTList<MemberNoteTreeInfo> list = MemberNoteTreeBL.ListSelect(3, memberId,"");// trả về <= 2 bản ghi
            if (list != null && list.Count > 0)
            {
                foreach (MemberNoteTreeInfo infoList in list)
                {
                    if (infoList.Position == "L")
                    {
                        sumLeft = MemberNoteTreeBL.CountNoteTree(infoList.IdMember.ToString());                       
                    }
                    else if (infoList.Position == "R")
                    {
                        sumRight = MemberNoteTreeBL.CountNoteTree(infoList.IdMember.ToString());                       
                    }
                }
            }
        }
    }
}